<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends MY_Controller {

	function index() {
		header("Location: /user/ulist");
	}

	function create() {
		$this->load->view('user/create', array('data' => $this->data));
	}	

	function create_do() {
		$current_user = $this->session->userdata('user_info');
		$data['operator'] = $current_user['id'];
		$data['username'] = $this->input->post('username', TRUE);
		$data['password'] = $this->input->post('password', TRUE);
		$data['atype'] = $this->input->post('atype', TRUE);
		$data['mobile'] = $this->input->post('mobile', TRUE);
		$data['email'] = $this->input->post('email', TRUE);
		$data['remark'] = $this->input->post('remark', TRUE);

		$this->load->model('cdn_user');
		$r = $this->cdn_user->filter($data);
		if ($r['status']){
			$user_id = $this->cdn_user->add($data);
			echo json_encode(array('r'=>'succ', 'msg' => $user_id));
		} else {
			echo json_encode(array('r'=>'fail', 'msg' => $r['msg']));
		}
	}

	function ulist($id = 0) {
		$this->load->model('cdn_user');
		$id = intval($id);
		$per_page = 10;
		$this->data['user'] = $this->cdn_user->all_user($id, $per_page);

		$config['base_url'] = '/user/ulist/';
		$config['total_rows'] = $this->cdn_user->utotal();
		$config['per_page'] = $per_page; 

		$this->load->library('pagination');
		$this->pagination->initialize($config); 

		$this->load->view('user/ulist', array('data' => $this->data));
	}

	function ulist_delete($id = 0) {
		if (intval($id) == 0) {
			echo json_encode(array('r' => 'fail', 'msg' => '用户ID为空'));
			return;
		}
		$this->load->model('cdn_user');
		$this->cdn_user->del(intval($id));
		echo json_encode(array('r' => 'succ', 'msg' => '操作成功'));
	}

	function glist($id = 0) {
		$this->load->model('cdn_user');
		$id = intval($id);
		$per_page = 10;
		$this->data['user'] = $this->cdn_user->all_group($id, $per_page);

		$config['base_url'] = '/user/ulist/';
		$config['total_rows'] = $this->cdn_user->gtotal();
		$config['per_page'] = $per_page; 

		$this->load->library('pagination');
		$this->pagination->initialize($config); 

		$this->load->view('user/glist', array('data' => $this->data));
	}

	function glist_delete($id = 0) {
		if (intval($id) == 0) {
			echo json_encode(array('r' => 'fail', 'msg' => '用户ID为空'));
			return;
		}
		$this->load->model('cdn_user');
		$this->cdn_user->del(intval($id));
		echo json_encode(array('r' => 'succ', 'msg' => '操作成功'));
	}

	function edit($id = 0) {
		$flag = TRUE;
		if (intval($id) == 0) $flag = FALSE;
		
		$this->load->model('cdn_user');
		$user = $this->cdn_user->find($id);
		if (!$user) {
			$flag = FALSE;
		} else {
			$this->data['user'] = $user[0];
		}
		
		$this->data['all_user'] = $this->cdn_user->all_user(0, 100000);
		$this->data['all_group'] = $this->cdn_user->all_group(0, 100000);

		$this->data['flag'] = $flag;
		$this->load->view('user/edit', array('data' => $this->data));
	}

	function edit_do() {
		$current_user = $this->session->userdata('user_info');
		//$data['username'] = $this->input->post('username', TRUE);
		$data['id'] = $this->input->post('id', TRUE);
		$data['password'] = $this->input->post('password', TRUE);
		if ($data['password'] == '') unset($data['password']);
		$data['atype'] = $this->input->post('atype', TRUE);
		$data['mobile'] = $this->input->post('mobile', TRUE);
		$data['email'] = $this->input->post('email', TRUE);
		$data['remark'] = $this->input->post('remark', TRUE);
		$data['operator'] = $current_user['id'];

		$this->load->model('cdn_user');
		$r = $this->cdn_user->efilter($data);
		if ($r['status']){
			$user_id = $this->cdn_user->up($data['id'], $data);
			echo json_encode(array('r'=>'succ', 'msg' => $user_id));
		} else {
			echo json_encode(array('r'=>'fail', 'msg' => $r['msg']));
		}
	}

	function group($id = 0) {
		$flag = TRUE;
		if (intval($id) == 0) $flag = FALSE;
		
		$this->load->model('cdn_user');
		$user = $this->cdn_user->find($id);
		if (!$user) {
			$flag = FALSE;
			$this->data['member'] = array();
			$this->data['leader'] = array();
		} else {
			$this->data['user'] = $user[0];
			$this->data['member'] = $this->cdn_user->member($user[0]['id']);
			$this->data['leader'] = $this->cdn_user->leader($user[0]['id']);
		}

		$this->data['flag'] = $flag;
		$this->data['all_user'] = $this->cdn_user->all_user(0, 100000);
		$this->data['all_group'] = $this->cdn_user->all_group(0, 100000);
		$this->load->view('user/group', array('data' => $this->data));
	}

	function group_join() {
		$data['user_id'] = $this->input->post('user_id', TRUE);
		$data['group_id'] = $this->input->post('group_id', TRUE);
		$data['role'] = $this->input->post('group_role', TRUE);
		$this->load->model('cdn_user');
		$r = $this->cdn_user->user_group_filter($data);
		if ($r['status']){
			$this->cdn_user->join_group($data);
			echo json_encode(array('r'=>'succ', 'msg' => $r['msg']));
		} else {
			echo json_encode(array('r'=>'fail', 'msg' => $r['msg']));
		}
	}

	function group_out($id = '') {
		if (intval($id) == '') {
			echo json_encode(array('r' => 'fail', 'msg' => '参数非法'));
			return;
		}
		$this->load->model('cdn_user');
		$tmp = explode('_', $id);
		$this->cdn_user->out_group(array('user_id' => $tmp[0], 'group_id' => $tmp[1]));
		echo json_encode(array('r' => 'succ', 'msg' => '操作成功'));
	}
}
